package com.prig.base.repository;

import com.prig.base.entity.Role;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;

@Repository
public interface RoleRepository extends JpaRepository<Role,Long> {
    @Query(value = "SELECT\n" +
            "\trole.id,\n" +
            "\trole.description,\n" +
            "\trole.role_name\n" +
            "FROM\n" +
            "\trole\n" +
            "INNER JOIN admin_role_relation ON admin_role_relation.role_id = role.id\n" +
            "WHERE\n" +
            "\tadmin_role_relation.admin_id = :adminId",nativeQuery = true)
    List<Role> findByAdminId(long adminId);
    @Query(value = "SELECT\n" +
            "\trole.id,\n" +
            "\trole.description,\n" +
            "\trole.role_name\n" +
            "FROM\n" +
            "\trole\n" +
            "INNER JOIN admin_role_relation ON admin_role_relation.role_id = role.id\n" +
            "WHERE\n" +
            "\tadmin_role_relation.admin_id = :adminId",nativeQuery = true)
    Page<Role> findByAdminId(Long adminId, Pageable pageable);
    @Transactional
    @Query(nativeQuery = true,value = "delete from role where id in :idsLong")
    @Modifying
    void deleteByIds(List<Long> idsLong);

    Page<Role> findByDescriptionLikeOrRoleNameLike(String keyword,String keyword2,Pageable pageable);
}
